package capricornus.components.button
{
	import capricornus.components.event.ComponentsEvent;
	import capricornus.components.label.Label;
	import capricornus.components.sprite.AbstractComponent;
	import capricornus.components.utils.style.StyleType;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	/**
	 * 触发按钮 
	 * @author kael
	 * 
	 */	
	public class TriggerButton extends AbstractComponent
	{
		public static var SELECT_TEXT_COLOR:uint = 0xFF9900;
		public static var OVER_TEXT_COLOR:uint = 0xffcc33;
		public static var UN_SELECT_TEXT_COLOR:uint = 0xffffff;
		
		/**
		 * disabled时候的皮肤类 
		 */		
		protected var _disabledSkinClass:Class;
		/**
		 * 鼠标up时候的皮肤类，也是平时无状态时候的样子 
		 */		
		protected var _upSkinClass:Class;
		/**
		 * 鼠标按下时候的皮肤类 
		 */		
		protected var _downSkinClass:Class;
		/**
		 * 鼠标经过时候的皮肤类 
		 */		
		protected var _overSkinClass:Class;
		
		/**
		 * 皮肤容器 
		 */		
		protected var _disabledSkinContainer:BtnSkin;
		protected var _upSkinContainer:BtnSkin;
		protected var _downSkinContainer:BtnSkin;
		protected var _overSkinContainer:BtnSkin;
		
		/**
		 * 点击区域 
		 */		
		protected var _hitSkinContainer:BtnSkin;
		
		protected var _currentSkin:BtnSkin;
		
		protected var _label:Label;
		
		public function TriggerButton()
		{
			super();
			init();
		}
		
		/**
		 * 一开始的时候设置各个皮肤类；如需修改，可重载此函数 
		 * 
		 */		
		protected function setSkinClass():void
		{
			_disabledSkinClass = Button_disabledSkin;
			_upSkinClass = Button_upSkin;
			_overSkinClass = Button_overSkin;
			_downSkinClass = Button_downSkin;
		}
		
		private function init():void
		{
			_disabledSkinContainer = new BtnSkin();
			_upSkinContainer = new BtnSkin();
			_downSkinContainer = new BtnSkin();
			_overSkinContainer = new BtnSkin();
			
			//设置皮肤
			setSkinClass();
			
			var dis:DisplayObject = disabledSin;
			if( dis != null ) _disabledSkinContainer.addChild( dis );
			dis = upSkin;
			if( dis != null ) _upSkinContainer.addChild( dis );
			dis = downSkin;
			if( dis != null ) _downSkinContainer.addChild( dis );
			dis = overSkin;
			if( dis != null ) _overSkinContainer.addChild( dis );
			
			currentSkin = _upSkinContainer;
		}
		
		public function destroy():void
		{
			if( parent ) parent.removeChild( this );
		}
		
		override public function setSize(w:int, h:int):void
		{
			super.setSize( w, h );
			_disabledSkinContainer.setSize( w, h );
			_overSkinContainer.setSize( w, h );
			_upSkinContainer.setSize( w, h );
			_downSkinContainer.setSize( w, h );
		}
		
		protected function get disabledSin():DisplayObject
		{
			if( _disabledSkinClass != null ) return new _disabledSkinClass() as DisplayObject;
			return null;
		}
		
		protected function get upSkin():DisplayObject
		{
			if( _upSkinClass != null ) return new _upSkinClass() as DisplayObject;
			return null;
		}
		
		protected function get downSkin():DisplayObject
		{
			if( _downSkinClass != null ) return new _downSkinClass() as DisplayObject;
			return null;
		}
		
		protected function get overSkin():DisplayObject
		{
			if( _overSkinClass != null ) return new _overSkinClass() as DisplayObject;
			return null;
		}
		
		/**
		 * 设置当前皮肤 
		 * @param value
		 * 
		 */		
		protected function set currentSkin( value:BtnSkin ):void
		{
			if( _currentSkin==value ) return;
			_currentSkin = value;
			
			var n:int = _container.numChildren;
			for( var i:int=n-1; i>=0; i-- )
			{
				if( _container.getChildAt(i) is BtnSkin )
				{
					_container.removeChildAt(i);
				}
			}
			_container.addChildAt( value, 0 );
		}
		
		override public function set enabled(value:Boolean):void
		{
			super.enabled = value;
			currentSkin = value?_upSkinContainer:_disabledSkinContainer;
			if( _label )
			{
				_label.textColor = UN_SELECT_TEXT_COLOR;
			}
		}
		
		override public function set selected(value:Boolean):void
		{
			if( selected != value )
			{
				super.selected = value;
				currentSkin = value?_downSkinContainer:_upSkinContainer;
				if( _label ) _label.textColor = value?SELECT_TEXT_COLOR:UN_SELECT_TEXT_COLOR;
			}
		}
		
		override public function set overSelected(value:Boolean):void
		{
			if( selected ) return;
			
			if( overSelected != value )
			{
				super.overSelected = value;
				currentSkin = value?_overSkinContainer:_upSkinContainer;
				if( _label ) _label.textColor = value?OVER_TEXT_COLOR:UN_SELECT_TEXT_COLOR;
			}
		}
		
		/**
		 * 设置按钮文本
		 * @param value
		 * 
		 */		
		public function set label( value:String ):void
		{
			if( _label==null )
			{
				_label = new Label();
				_label.textColor = 0xffffff;
				_label.mouseChildren = false;
				_label.mouseEnabled = false;
				_container.addChild( _label );
			}
			_label.text = value;
			if( _isAutoSize )
			{
				setSize( _label.width + 8, _label.height + 4 );
				autoSize();
			}
			resetLabel();
			
			if( _isAutoSize ) dispatchEvent( new ComponentsEvent( ComponentsEvent.RESIZE ) );
		}
		
		/**
		 * 设置皮肤样式 
		 * @param name
		 * @param style
		 * 
		 */		
		public function setStyle( name:String, style:Object ):void
		{
			var mc:BtnSkin = null;
			
			if( style is Class ) style = new style();
			
			if( name==StyleType.BTN_UP_STYLE_SKIN )
			{
				mc = _upSkinContainer;
			}
			else if( name==StyleType.BTN_DOWN_STYLE_SKIN )
			{
				mc = _downSkinContainer;
			}
			else if( name==StyleType.BTN_OVER_STYLE_SKIN )
			{
				mc = _overSkinContainer;
			}
			else if( name==StyleType.HIT_STYLE_SKIN )
			{
				setHitState( style as Sprite );
			}
			if( mc != null )
			{
				mc.resetSin( style as DisplayObject );
			}
		}
		
		protected function resetLabel():void
		{
			if( _label )
			{
				_label.x = (width - _label.width)>>1;
				_label.y = (height - _label.height)>>1;
			    _label.textColor = UN_SELECT_TEXT_COLOR;
			}
		}
		
		protected function setHitState( dis:Sprite ):void
		{
			if( dis != null && dis.width != 0 && dis.height != 0 )
			{
				if( _hitSkinContainer==null ) _hitSkinContainer = new BtnSkin();
				_hitSkinContainer.resetSin( dis );
				this.addChild( _hitSkinContainer );
				this.hitArea = _hitSkinContainer;
			}
			else
			{
				if( _hitSkinContainer && _hitSkinContainer.parent ) _hitSkinContainer.parent.removeChild( _hitSkinContainer );
				this.hitArea = null;
			}
		}
	}
}

























